﻿'Clinciu Andrei

Option Strict On

Imports Gift.BL
Imports System.Configuration

Public Class frmOrdersWizard

    Private _currentForm As Integer = 1
    Private _bestId As Integer
    Private _products As List(Of OrderGiftListProd)
    Private _type As String = "New"
    Private _paymentType As String

    Public Property Products As List(Of OrderGiftListProd)
        Get
            Return _products
        End Get

        Set(ByVal value As List(Of OrderGiftListProd))
            _products = value
        End Set
    End Property

    Public Sub EditWizard(ByVal bestId As Integer, ByVal paymentType As String)
        _currentForm = 4
        _bestId = bestId
        _type = "Edit"
        _paymentType = paymentType
        ShowWizard(4)
    End Sub


    Public Sub ShowWizard(ByVal number As Integer)
        'show wizards
        If number = 1 Then
            pnlWizard1.Show()
            pnlWizard2.Hide()
            pnlWizard3.Hide()

            'buttons handling
            ShowButtons()

            'Load from database
            If cboList.Items.Count = 0 Then
                Dim list As List(Of GiftList) = GiftList.GetGiftListsFromDatabase(frmMain.dbh)
                For Each item As GiftList In list
                    cboList.Items.Add(item)
                Next
            End If
        ElseIf number = 2 Then
            pnlWizard1.Hide()
            pnlWizard2.Show()
            pnlWizard3.Hide()

            ShowButtons()

            'Load products from the database
            FillProducts()

        ElseIf number = 3 Then
            pnlWizard1.Hide()
            pnlWizard2.Hide()
            pnlWizard3.Visible = True

            ShowButtons()
            'Get the owners
            GetOwners()
            If cboPaymentType.Items.Count = 0 Then
                For Each payment As PaymentType In PaymentType.GetAllPaymentTypes(frmMain.dbh)
                    cboPaymentType.Items.Add(payment)
                Next
            End If
        ElseIf number = 4 Then
            pnlWizard1.Hide()
            pnlWizard2.Show()
            pnlWizard3.Hide()

            ShowButtons()
            'Get the products this time from the order
            Console.WriteLine(_bestId)
            Products = Order.GetOrderProd(frmMain.dbh, _bestId)
            Dim list As List(Of OrderGiftListProd) = Products
            For Each item As OrderGiftListProd In list
                lstProducts.Items.Add(item)
                Console.WriteLine(item)
            Next

            'Get payment method
            If cboBetalingsTypeEdit.Items.Count = 0 Then
                For Each payment As PaymentType In PaymentType.GetAllPaymentTypes(frmMain.dbh)
                    cboBetalingsTypeEdit.Items.Add(payment)
                    If _paymentType = payment.Name Then
                        cboBetalingsTypeEdit.SelectedIndex = cboBetalingsTypeEdit.FindString(_paymentType)
                    End If
                Next
            End If
            'Paid or not
            chkPaid.Checked = False
        End If
    End Sub

    'enable disable the buttons..
    Private Sub ShowButtons()
        If _currentForm = 1 Then
            btnPrevious.Enabled = False
            btnNext.Enabled = True
            btnOk.Enabled = False
        ElseIf _currentForm = 2 Then
            btnPrevious.Enabled = True
            btnNext.Enabled = True
            btnOk.Enabled = False
            lblProductsError.Visible = False
        ElseIf _currentForm = 3 Then
            btnNext.Enabled = False
            If Not cboOwner.SelectedItem Is Nothing Then
                btnOk.Enabled = True
            End If

        ElseIf _currentForm = 4 Then
            'The edit form.. showing all extra options:)
            btnPrevious.Hide()
            btnNext.Hide()
            lblPaymentType.Visible = True
            cboBetalingsTypeEdit.Visible = True
            chkPaid.Visible = True
        End If
    End Sub

    'switch between wizards
    Private Sub SwitchWizard(ByVal type As String)
        If type = "Next" Then
            _currentForm += 1
            ShowWizard(_currentForm)
        ElseIf type = "Previous" Then
            _currentForm -= 1
            ShowWizard(_currentForm)
        End If
    End Sub

    Private Function Validation() As Boolean
        Dim isValid As Boolean = True
        Dim howMany As Integer = 0
        'cboLijst => wiz 1
        'lstProducts & nudTotal => Wiz 2
        'cboOwner => wiz3
        Select Case _currentForm
            Case 1
                If cboList.SelectedIndex = -1 Then
                    isValid = False
                    MessageBox.Show("Selecteer een geschenklijst om verder te gaan.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Case 2
                For Each item As OrderGiftListProd In lstProducts.Items
                    If item.TotalOrdered <> 0 Then
                        howMany += 1
                    End If
                Next
                'Show a messagebox
                If howMany = 0 Then
                    isValid = False
                    MessageBox.Show("U moet minstens een product bestellen met een waarde groter dan 0.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
        End Select
        'return the validation
        Return isValid
    End Function

    Private Sub GetOwners()
        cboOwner.Items.Clear()
        For Each Owner As Person In Person.GetPersonsFromDatabase(frmMain.dbh)
            cboOwner.Items.Add(Owner)
        Next
    End Sub

    Private Sub FillProducts()
        If lstProducts.Items.Count = 0 Then
            Products = Order.GetGiftListProd(frmMain.dbh, CType(cboList.SelectedItem, GiftList).Id)
            Dim list As List(Of OrderGiftListProd) = Products
            For Each item As OrderGiftListProd In list
                lstProducts.Items.Add(item)
            Next
        End If
    End Sub

#Region "Events"

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim goAhead As Boolean = True
        If _type = "Edit" Then
            'check if everything is OK

        End If
        If goAhead Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ' User clicked the Cancel button
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If Validation() Then
            SwitchWizard("Next")
        End If
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        SwitchWizard("Previous")
    End Sub

    Private Sub btnNew_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim dialog As New frmPersonEditAdd

        If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            'reload the users in the combobox
            GetOwners()
        End If
    End Sub

    'Show the values each time an item is selected
    Private Sub lstProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProducts.SelectedIndexChanged
        nudTotal.Enabled = True
        nudTotal.Value = CType(lstProducts.SelectedItem, OrderGiftListProd).TotalOrdered
        txtMaximum.Text = CStr(CType(lstProducts.SelectedItem, OrderGiftListProd).MaxOrdered)
    End Sub

    'check if the values are correct and show error label. if the value is higher than the maximum set it to the maximum
    Private Sub nudTotal_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles nudTotal.ValueChanged

        If CType(lstProducts.SelectedItem, OrderGiftListProd).MaxOrdered < nudTotal.Value Then
            CType(lstProducts.SelectedItem, OrderGiftListProd).TotalOrdered = CType(lstProducts.SelectedItem, OrderGiftListProd).MaxOrdered
            lblProductsError.Visible = True
        Else
            lblProductsError.Visible = False
            CType(lstProducts.SelectedItem, OrderGiftListProd).TotalOrdered = CInt(nudTotal.Value)
        End If

    End Sub

    Private Sub cboOwner_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboOwner.SelectedIndexChanged, cboPaymentType.SelectedIndexChanged
        If Not cboOwner.SelectedItem Is Nothing AndAlso Not cboPaymentType.SelectedItem Is Nothing Then
            Console.WriteLine("You selected something!")
            btnOk.Enabled = True
        End If
    End Sub
#End Region


End Class